home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS20.ADF
/
BobEd
/
disk.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-01-27
|
6KB
|
199 lines
#include <bobed.h>
#include <lattice/fcntl.h>
/*
"disk.c"
BobEd disk functions. V1.0 uses lattice level I I/O functions.
*/
#define EOF (-1)
#define WRITEMODE (O_CREAT | O_WRONLY) /* create if it does not exist */
#define READMODE O_RDONLY /* read only */
int save ();
int load ();
VOID hexword ();
extern UBYTE bobdata [OBS+1][BWIDE*16][BHIGH];
/* these definitions are for saving as c-code which can be compiled
(almost) directly. One bug to look for is the last word of data
will have a "," which C does not like to see. it can be removed
with Ed by issuing : t;f/};/;p;p;ce;cl;dc
on the file produced by BobEd.
*/
/*
the char definitions below are for comments and syntax within the
c code data statements.
*/
char comm [] = "/* -- -- */"; /* 0-2, 10-3, 0-13 */
char come [] = " -- */";
char wide [] = "#define WIDE ";
char high [] = "#define HIGH ";
char deep [] = "#define DEEP ";
char def [] = "SHORT name [] = {";
char edef [] = "};";
char cr [] = {0x0a};
char s [] = {'0','x','0','0','0','0',',','\0'};
char hex [] = "0123456789ABCDEF";
UWORD mask [] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
int save (savec,name,v,x0,y0,x1,y1,bp)
char name [];
SHORT savec,v,x0,y0,x1,y1,bp;
{
int i,x,y,p;
int bi; /* buffer index */
int file;
int len;
UWORD val;
UBYTE a;
UBYTE buf [BWIDE*16];
file = open (name, WRITEMODE);
if (file == EOF) /* error in creating file */
return (FALSE);
buf [0] = 0xff; /* flag that this is bin-data not c-program */
buf [1] = x0; /* buffer sizes for write */
buf [2] = y0;
buf [3] = x1;
buf [4] = y1;
if (savec == FALSE) /* save as binary */
{
len = write (file, buf, 5); /* write sizes */
bp = 1;
}
if (savec)
{
len = write (file, comm,6); /* write name of image */
len = write (file, name,strlen(name));
len = write (file, come,6);
len = write (file, cr,1);
len = write (file, wide, strlen(wide));/* write word width of image */
a = stcu_d (buf, (x1-x0+15)/16,len);
len = write (file, buf,a);
len = write (file, cr,1);
len = write (file, high, strlen(high)); /* write height of image */
a = stcu_d (buf, y1-y0, len);
len = write (file, buf, a);
len = write (file, cr,1);
len = write (file, deep, strlen(deep)); /* write depth of image */
a = stcu_d (buf, bp, len);
len = write (file, buf, a);
len = write (file, cr,1);
len = write (file, cr,1);
len = write (file, def,strlen(def)); /* write SHORT name [] = */
len = write (file, cr,1);
}
for (p=0;p<bp;p++) /* do bp planes */
{
for (y=y0;y<y1;y++)
{
if (savec) /* save in words */
{
for (x=x0;x<x1;x+=16)
{
val=0;
for (i=0;i<16;i++) /* one bit at a time */
{
a = (bobdata[v][x+i][y] & mask[p])/mask[p]; /* mask bit */
val += a*mask[15-i]; /* put into one word */
}
hexword (val); /* convert to hex string */
len = write (file,s,7); /* write hex word */
}
}
else /* save as binary. The method is rather inefficient but
easy. the data is arranged as 1 byte per pixel which
represents the color of that pixel. therefore maximum
size of file is 3*16*45=2160 bytes. since the max colors
in this progam is 16 it we half that size by packing
two nibbles into each byte by shifting the second nybble
into the upper 4 bits of the byte ( << 4).
*/
{
for (x=x0,bi=0;x<x1;bi++,x+=2) /* do a line at a time, two bytes at a time */
buf [bi] = ((UBYTE)bobdata [v][x][y] | (UBYTE)bobdata [v][x+1][y] << 4);
if (((x1-x0) % 2) > 0) /* if we went one byte too far */
buf [bi-1] = buf [bi-1] & 15; /* blank out top bits */
len = write (file,buf,(x1-x0)/2);
}
if (savec)
len = write (file,cr,1); /* put a <CR> at the end of each line */
}
if (savec) /* write a divider between planes */
{
len = write (file, comm, strlen(comm));
len = write (file, cr,1);
}
}
if (savec) /* write the }; at EOF */
{
len = write (file, edef,2);
len = write (file, cr,1);
len = write (file, cr,1);
}
len = close (file); /* close er up */
return (TRUE);
}
VOID hexword (w)
UWORD w;
{
int i;
for (i=0;i<4;i++)
{
s [5-i] = hex[w & 15];
w = w>>4;
}
}
int load (v,name)
SHORT v;
char *name;
{
SHORT x,y;
UBYTE x0,y0,x1,y1;
char buf [5];
SHORT len;
int file;
file = open (name, READMODE);
if (file == EOF)
return (FALSE);
len = read (file, buf, 5);
if (buf[0] > 0)
{
len = close (file);
return (FALSE);
}
x0 = buf [1];
y0 = buf [2];
x1 = buf [3];
y1 = buf [4];
for (y=y0;y<y1;y++)
for (x=x0;x<x1;x+=2)
{
len = read (file,buf,1);
bobdata [v][x][y] = buf [0] & 15; /* blank out top nybble */
bobdata [v][x+1][y] = (buf [0] & ~15) >> 4; /* blank bottom and
shift into lower
nybble */
}
len = close (file);
return (TRUE);
}